{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 用assert检查条件是否为真\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 定义除法函数\n",
    "def divide(a, b):\n",
    "    assert b != 0, \"除数不能为零\"\n",
    "    return a / b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:04:31.511673Z",
     "end_time": "2024-07-03T16:04:31.516304Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "除数不能为零",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAssertionError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[2], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 调用自定义函数\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m result \u001B[38;5;241m=\u001B[39m \u001B[43mdivide\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m10\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;66;03m# 除以零，会引发 AssertionError 异常\u001B[39;00m\n",
      "Cell \u001B[1;32mIn[1], line 3\u001B[0m, in \u001B[0;36mdivide\u001B[1;34m(a, b)\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mdivide\u001B[39m(a, b):\n\u001B[1;32m----> 3\u001B[0m     \u001B[38;5;28;01massert\u001B[39;00m b \u001B[38;5;241m!=\u001B[39m \u001B[38;5;241m0\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m除数不能为零\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m      4\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m a \u001B[38;5;241m/\u001B[39m b\n",
      "\u001B[1;31mAssertionError\u001B[0m: 除数不能为零"
     ]
    }
   ],
   "source": [
    "# 调用自定义函数\n",
    "result = divide(10, 0)\n",
    "# 除以零，会引发 AssertionError 异常"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'result' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 除以零，会引发 AssertionError 异常\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mresult\u001B[49m)\n\u001B[0;32m      3\u001B[0m \u001B[38;5;66;03m# 上行不会被执行，因为异常已经引发\u001B[39;00m\n",
      "\u001B[1;31mNameError\u001B[0m: name 'result' is not defined"
     ]
    }
   ],
   "source": [
    "# 除以零，会引发 AssertionError 异常\n",
    "print(result)\n",
    "# 上行不会被执行，因为异常已经引发"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
